package com.dataprocessor.util;

import java.io.*;

public class TextUtils {

	// 生成的txt路径配置
	private static String TXTNUMPATH = "D:\\anchorsSpiderBaikeTxt\\wordNum.txt";

	/**
	 * 创建文件
	 * 
	 * @param fileName
	 * @return
	 */
	public static boolean createFile(File fileName) throws Exception {
		try {
			if (!fileName.exists()) {
				System.out.println("创建文件：：：："+fileName.getPath());

				fileName.createNewFile();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	public static void createFile() {
		String strPath = "D:\\anchorsSpiderBaikeTxt\\wordNum.txt";
		File file = new File(strPath);
		if (!file.exists()) {
			file.mkdirs();
		}

		String strPath2 = "D:\\anchorsSpiderBaikeTxt\\spiderWordNames.txt";
		File file2 = new File(strPath);
		if (!file2.exists()) {
			file2.mkdirs();
		}
	}

	/**
	 * 读取TXT内容
	 * 
	 * @param file
	 * @return
	 */
	public static String readTxtFile(File file) {
		String result = "";
		try {
			InputStreamReader reader = new InputStreamReader(
					new FileInputStream(file), "utf-8");
			BufferedReader br = new BufferedReader(reader);
			String s = null;
			while ((s = br.readLine()) != null) {
				result = result +"|"+ s;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 写入TXT，覆盖原内容
	 * 
	 * @param content
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static boolean writeTxtFile(String content, File fileName)
			throws Exception {
		RandomAccessFile mm = null;
		boolean flag = false;
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(fileName);
			fileOutputStream.write(content.getBytes("utf-8"));
			fileOutputStream.close();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	public static String getWaiHtml(String doc) {
		String html = "<!DOCTYPE html><html lang=\"zh-CN\"><head><meta charset=\"utf-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"format-detection\" content=\"telephone=no\"><link href=\"global.css\" rel=\"stylesheet\" />"
				+ doc + "</head><body></body></html>";
		return html;
	}

	/**
	 * 写入TXT，追加写�?
	 * 
	 * @param filePath
	 * @param content
	 */
	// public static boolean fileChaseFW(String filePath, String item,
	// String newLemmaId, String version, String content,
	// ArrayBlockingQueue<String> DocQueue, Object lock) {
	// try {
	// // 构�?函数中的第二个参数true表示以追加形式写文件
	// synchronized (lock) {
	// OutputStreamWriter fw = new OutputStreamWriter(
	// new FileOutputStream(new File(filePath), true), "UTF-8");
	// if (!StrUtils.isEmpty(item)) {
	// fw.write(item
	// +
	// "\r\n<!DOCTYPE html><html lang=\"zh-CN\"><head><meta charset=\"utf-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"format-detection\" content=\"telephone=no\"><link href=\"global.css\" rel=\"stylesheet\" /></head><body>"
	// + content
	// + "<script src=\"global.js\"></script></body></html>\r\n</>\r\n");
	// }
	//
	// System.out.println("正在写入词条" + item);
	// // String epath = "D:\\wordNum.txt";
	// InitSpiderFile.NOW_VALUE++;
	// String txtContent = InitSpiderFile.NOW_VALUE + ","
	// + InitSpiderFile.SOURE_ID;
	// try {
	// String txtNumPath = creatFile(
	// "D:\\anchorsSpiderBaikeTxt\\", "wordNum.txt");
	// TextUtils.writeTxtFile(txtContent, new File(txtNumPath));
	// } catch (Exception e) {
	// }
	// LoadAndWrite.writeTxt(
	// creatFile("D:\\anchorsSpiderBaikeTxt\\",
	// "spiderWordNames.txt"), item, newLemmaId,
	// version);
	//
	// try {
	// System.out.println("释放...");
	// DocQueue.take();
	// } catch (InterruptedException e) {
	// e.printStackTrace();
	// }
	// fw.close();
	// }
	// // try {
	// // // DocQueue.take();
	// // } catch (InterruptedException e) {
	// // Logger.info("已没有队列可释放");
	// // e.printStackTrace();
	// // }
	// } catch (IOException e) {
	// System.out.println("文件写入失败词条" + item);
	// }
	// return true;
	// }

	/**
	 * 
	 * 创建别写�?
	 * 
	 * @param FileUrl
	 * @param content
	 * @throws Exception
	 * @author huangzhubing
	 */
	public static void createSessText(String FileUrl, String content)
			throws Exception {
		File file = new File(FileUrl);
		createFile(file);
//		readTxtFile(file);
		writeTxtFile(content, file);
		// fileChaseFW(FileUrl, content);
	}

	/**
	 * 尾部追加id
	 * 
	 * @param content
	 */
	public static boolean appendTxt( String content,String path) {
		try {
			File file = new File(path);
			if(!file.exists()){
				file.createNewFile();
			}
			// 函数中的第二个参数true表示以追加形式写文件
			FileWriter fw = new FileWriter(path, true);
			System.out.println(content);
			fw.write(content + "");
			fw.close();
		} catch (IOException e) {
			System.out.println(e.getMessage());
			System.out.println("文件写入失败" + content);
		}
		return true;
	}

	/**
	 * 指定路径创建文件
	 * 
	 * @param filePath
	 * @param fileName
	 */
	public static String creatFile(String filePath, String fileName) {
		// System.out.println("创建.." + filePath + fileName);
		String pathName = filePath + fileName;
		File folder = new File(filePath);
		// 文件夹路径不存在
		if (!folder.exists() && !folder.isDirectory()) {
			System.out.println("文件夹路径不存在，创建路径:" + filePath);
			folder.mkdirs();
		} else {
			// System.out.println("文件夹路径存在:" + filePath);
			return pathName;
		}

		// 如果文件不存在就创建

		File file = new File(pathName);
		if (!file.exists()) {
			System.out.println("文件不存在，创建文件:" + pathName);
			try {
				System.out.println("创建文件：：：："+file.getPath());
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			// System.out.println("文件已存在，文件为:" + filePath + fileName);
		}
		return pathName;
	}

	public static void main(String[] arg) {
	}

}
